package com.harmonyos.samples.view;

import com.harmonyos.samples.bean.LocationBean;

import java.awt.*;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;

public class WaterFlowLayout implements LayoutManager {
    private final int itemWidth;
    private final int spacing;
    private int columnNumber = 3;
    private List<LocationBean> locationList = new ArrayList<>();

    public WaterFlowLayout(int itemWidth, int spacing, int columnNumber) {
        this.columnNumber = columnNumber;
        this.itemWidth = itemWidth;
        this.spacing = spacing;
        for (int i = 0; i < columnNumber; i++) {
            LocationBean location = new LocationBean(spacing + i * (itemWidth + spacing), spacing);
            locationList.add(location);
        }
    }

    public WaterFlowLayout(int itemWidth, int spacing) {
        this.itemWidth = itemWidth;
        this.spacing = spacing;
        for (int i = 0; i < columnNumber; i++) {
            LocationBean location = new LocationBean(spacing + i * (itemWidth + spacing), spacing);
            locationList.add(location);
        }
    }

    public void reset() {
        locationList.clear();
        for (int i = 0; i < columnNumber; i++) {
            LocationBean location = new LocationBean(spacing + i * (itemWidth + spacing), spacing);
            locationList.add(location);
        }
    }

    @Override
    public void addLayoutComponent(String s, Component component) {

    }

    @Override
    public void removeLayoutComponent(Component component) {

    }


    @Override
    public Dimension preferredLayoutSize(Container container) {

        reset();
        for (Component component : container.getComponents()) {
            locationList.sort(Comparator.comparingInt(LocationBean::getY));
            int y = locationList.get(0).getY();
            int itemHeight = component.getPreferredSize().height;
            y = y + itemHeight + spacing / 4;
            locationList.get(0).setY(y);
        }

        int totalWidth = itemWidth * columnNumber + (spacing * columnNumber + 1);

        int max = 0;
        for (int i = 0; i < locationList.size(); i++) {
            max = Math.max(max, locationList.get(i).getY());
        }
        int totalHeight = max;
        reset();
        return new Dimension(totalWidth, totalHeight);
    }

    @Override
    public Dimension minimumLayoutSize(Container container) {
        return new Dimension(1000, 800);
    }

    @Override
    public void layoutContainer(Container container) {
        for (Component component : container.getComponents()) {
            locationList.sort(Comparator.comparingInt(LocationBean::getY));
            int x = locationList.get(0).getX();
            int y = locationList.get(0).getY();
            int itemHeight = component.getPreferredSize().height;
            component.setBounds(x, y, itemWidth, itemHeight);
            y = y + itemHeight + spacing / 4;
            locationList.get(0).setY(y);
        }
    }

    public int getColumnNumber() {
        return columnNumber;
    }

}
